package com.example.openAI;

import java.util.ArrayList;
import java.util.List;

public class Util {
    public static double dotProduct(List<Float> vec1, List<Float> vec2) {
        double product = 0.0;
        int size = Math.min(vec1.size(), vec2.size());
        for (int i = 0; i < size; i++) {
            product += vec1.get(i) * vec2.get(i);
        }
        return product;
    }

    public static double norm(List<Float> vec) {
        double sumOfSquares = 0.0;
        for (double value : vec) {
            sumOfSquares += value * value;
        }
        return Math.sqrt(sumOfSquares);
    }

    public static double cosineSimilarity(List<Float> vec1, List<Float> vec2) {
        double dotProduct = dotProduct(vec1, vec2);
        double normVec1 = norm(vec1);
        double normVec2 = norm(vec2);

        if (normVec1 == 0 || normVec2 == 0) {
            return 0.0; // Handle division by zero
        }

        return dotProduct / (normVec1 * normVec2);
    }

}
